package com.jinzht.tools;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.servlet.http.HttpSession;

import com.wx.tools.AKConfig;

public class Tools {

	public static String objToStr(String obj) {
		if (obj != null && obj != "") {
			return obj.trim();
		}
		return "";

	}

	/***
	 * 根据内容id生成网页查看链接
	 * 
	 * @param contentId
	 * @return
	 */
	public static String generateWebRecordUrl(Integer contentId) {
		String path = String.format("%swebUrlLooker.action?contentId=%d",
				Config.STRING_SYSTEM_ADDRESS, contentId);
		return path;
	}

	/***
	 * 按照密码生成规则生成密码
	 * 
	 * @param password
	 * @return
	 */
	public static String generatePassword(String password, String telephone) {
		String str = String.format(Config.STRING_PASWWORD_RULE, password,
				telephone);

		return MD5.GetMD5Code(str);
	}

	public static void sendMessage(String content, String telephone) {
		Map<String, String> para = new HashMap<String, String>();

		MsgUtil SMS = new MsgUtil(telephone, content, MessageType.NormalMessage);
		SMS.send();
	}

	public static String getRandomString(int length) { // length表示生成字符串的长度
		String base = "abcdefghijklmnopqrstuvwxyz0123456789";
		Random random = new Random();
		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < length; i++) {
			int number = random.nextInt(base.length());
			sb.append(base.charAt(number));
		}
		return sb.toString();
	}

	public static void setValueOfWebPage(Map map, Integer page, Integer count,
			Integer size, Integer sortmenu, Integer menu, Integer submenu) { // length表示生成字符串的长度

		if (size != 0 && count != 0) {
			List pages = new ArrayList();
			Integer num = count / size;
			if (page < num) {
				if (page < 5) {
					if (num - page > 5) {
						for (int i = 0; i < 5; i++) {
							pages.add(i);
						}
					} else {
						for (int i = 0; i <= num; i++) {
							pages.add(i);
						}
					}
				} else {
					for (int i = page; i < page + 5; i++) {
						pages.add(i);
					}
				}
			} else {
				page = num;

				for (int i = 0; i <= page; i++) {
					pages.add(i);
				}
			}
			map.put("pages", pages);
			map.put("count", count / size);
			map.put("page", page);
			map.put("size", size);
		}

		if (sortmenu == null) {
			sortmenu = 0;
		}

		map.put("sizes", Config.SIZES);
		map.put("sortmenu", sortmenu);
		map.put("menu", menu);
		map.put("submenu", submenu);
	}

	public static boolean isChinese(String str) {
		String regEx = "[\u4e00-\u9fa5]";
		Pattern pat = Pattern.compile(regEx);
		Matcher matcher = pat.matcher(str);
		boolean flg = false;
		if (matcher.find())
			flg = true;

		return flg;
	}

	public static Double M2(Double f) {
		BigDecimal bg = new BigDecimal(f);
		double f1 = bg.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
		System.out.println(f1);
		return f1;
	}

}
